<?php
/**
 * Celebrio Kernel
 *
 * @copyright  Copyright (c) 2012 Celebio Software, s.r.o.
 * @package    Kernel
 */
namespace KernelModule\LogsModule\PerformanceModule;

use Nette\Object;

use Celebrio\Core\Config;
use Celebrio\Diagnostics\TimerLevel;

/**
 * Manager for performance log levels in Celebrio
 * 
 * @author Martin Novák
 */
class LogLevelManager extends Object {
	
	/**
	 * (TimerLevel::REQUEST | TimerLevel::OVERVIEW | default TimerLevel::DETAILED)
	 * @var int
	 */
	private static $level = NULL;
	
	/**
	 * gets current performance log level
	 * (TimerLevel::REQUEST | TimerLevel::OVERVIEW | default TimerLevel::DETAILED)
	 * @return int
	 */
	public static function getCurrent() {
		if(self::$level != NULL) return self::$level;
		
		$configArray = Config::current()->loadConfigArray("TimersLogLevel");
		if(!$configArray){
			self::$level = TimerLevel::REQUEST;
			return TimerLevel::REQUEST;
		}
		$level = $configArray["level"];
		if($level != TimerLevel::REQUEST && $level != TimerLevel::OVERVIEW && $level != TimerLevel::DETAILED){
			self::$level = TimerLevel::REQUEST;
			return TimerLevel::REQUEST;
		} else {
			self::$level = $level;
			return $level;
		}
	}
	
	/**
	 * Sets new performance log level
	 * (TimerLevel::REQUEST | TimerLevel::OVERVIEW | default TimerLevel::DETAILED)
	 * @param int $level
	 * @return bool
	 */
	public static function setLogLevel($level) {
		if($level != TimerLevel::REQUEST && $level != TimerLevel::OVERVIEW && $level != TimerLevel::DETAILED){
			return FALSE;
		}
		Config::current()->saveConfigArray(array("level" => $level),"TimersLogLevel");
		self::$level = $level;
		return TRUE;
	}
	
}